package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.cache.service.IScenicCommentStaticVOService;
import cn.wolfcode.luowowo.cache.service.IScenicStaticVOService;
import cn.wolfcode.luowowo.cache.vo.ScenicStaticVO;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.scenic.domain.Scenic;
import cn.wolfcode.luowowo.scenic.domain.ScenicTag;
import cn.wolfcode.luowowo.scenic.service.IScenicService;
import cn.wolfcode.luowowo.scenic.service.IScenicTagService;
import cn.wolfcode.luowowo.scenic.service.ISurveyService;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("scenic")
public class ScenicController {
    @Reference
    private IScenicService scenicServic;
    @Reference
    private ISurveyService surveyService;
    @Reference
    private IScenicTagService scenicTagService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicStaticVOService scenicStaticVOService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicCommentStaticVOService scenicCommentStaticVOService;

    @RequestMapping("/addcomment")
    @ResponseBody
    public Object addcomment(Model model, ScenicComment scenicComment, @UserParam UserInfo userInfo){
        if (userInfo==null){
            AjaxResult result=new AjaxResult(false,"请登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return result;
        }
        scenicComment.setHeadUrl(userInfo.getHeadImgUrl());
        scenicComment.setLevel(userInfo.getLevel());
        scenicComment.setUserId(userInfo.getId());
        scenicComment.setUsername(userInfo.getNickname());
        ScenicComment comment = scenicCommentService.save(scenicComment);
        Integer count=scenicCommentService.getCommentCount(scenicComment.getScenicDetailId());
        scenicServic.updateReplyNum(count,scenicComment.getScenicDetailId());
       // model.addAttribute("c",comment);
        return AjaxResult.success();

    }

    @RequestMapping("list")
    public Object list(Model model, Long destId,Long tagId){


        model.addAttribute("destTd");
        //通过目的地id 找到目的地的概况
            String string=surveyService.getSurveyByDestId(destId);
        if(string!=null){
            model.addAttribute("survey",string);
        }
        //必游景点TOP5
        List<Scenic> list= scenicServic.getScenicByDestId(destId);
        //遍历查找出必游景点的下关联的小景点,
        for (Scenic scenic : list) {
            //查询该景点的评论数量
            Integer count=scenicCommentService.getCommentCount(scenic.getId());

            scenic.setReplynum(count);
            Long scenicId = scenic.getId();
            List<Scenic> scenics = scenicServic.getScenicByParentId(scenicId);
            List<String> coverImgUrl=scenicServic.getCoverImgUrl(scenic);
            scenic.setCoverImgUrl(coverImgUrl);
            scenic.setSubScenic(scenics);
        }
        model.addAttribute("list",list);

        //热门景点,第二个参数为热门的参数
        List<Scenic> hotScenic=scenicServic.getHotScenicByDestId(destId,Scenic.SCENIC_HOT);
        for (Scenic scenic : hotScenic) {
            List<String> coverImgUrl = scenicServic.getCoverImgUrl(scenic);
            scenic.setCoverUrl(coverImgUrl.get(0));
        }
        model.addAttribute("hotScenic",hotScenic);


        //查询景点主题
        List<ScenicTag> scenicTagLis=scenicTagService.getScenicTag(destId);
        model.addAttribute("tags",scenicTagLis);



        //目的全部景点
        List<Scenic>  scenicsAll=scenicServic.getByDestId(destId);
        for (Scenic scenic : scenicsAll) {
            List<String> imgUrl = scenicServic.getCoverImgUrl(scenic);
            scenic.setCoverUrl(imgUrl.get(0));
        }
        scenicsAll = scenicsAll.subList(0, 10);


        model.addAttribute("scenicsAll",scenicsAll);
        return AjaxResult.success();

    }


    @RequestMapping("detail")
    public void scenicIdDetail(Model model,Long scenicId,@UserParam UserInfo userInfo){
        Scenic detail = scenicServic.getDetail(scenicId);
        List<String> coverImgUrl = scenicServic.getCoverImgUrl(detail);
        model.addAttribute("coverImgUrl",coverImgUrl);
        model.addAttribute("scenicDetail",detail);

        //查询内部景点
        List<Scenic> scenics=scenicServic.getScenicByInterior(scenicId);
        for (Scenic scenic : scenics) {
            List<String> imgUrl = scenicServic.getCoverImgUrl(scenic);
            for (String s : imgUrl) {
                scenic.setCoverUrl(s);
            }
        }
        //评论查询列表
        List<ScenicComment> scenicComments=scenicCommentService.list(scenicId);
        //List<ScenicComment> scenicSmallComments=scenicCommentService.listSmall(scenicId);
        //查询评论数量
        Integer count=scenicCommentService.getCommentCount(scenicId);
        model.addAttribute("count",count);
        model.addAttribute("scomment",scenicComments);
        model.addAttribute("interior",scenics);
        //获取redis中景点的数据统计vo对象
        ScenicStaticVO scenicStaticVo = scenicStaticVOService.getScenicStaticVo(scenicId);
        model.addAttribute("vo",scenicStaticVo);
        /*if(userInfo!=null){
            boolean isfavor = scenicStaticVOService.isFavor(userInfo.getId(), scenicId);
            model.addAttribute("isfavor",isfavor);
        }*/
    }

    //收藏和取消收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(@UserParam UserInfo userInfo,Long sid){
        //点击收藏前先判断用户是否登录
        if (userInfo==null){
            AjaxResult result=new AjaxResult(false,"请先登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return  result;
        }else {
            boolean ret=scenicStaticVOService.favor(userInfo.getId(),sid);
            return  new AjaxResult(ret,"").addData(scenicStaticVOService.getScenicStaticVo(sid));
        }
    }


    //点评的点赞
    @RequestMapping("/commentThum")
    @ResponseBody
    public Object commentThum(@UserParam UserInfo userInfo,String sid){
        if (userInfo==null){
            AjaxResult result=new AjaxResult(false,"请先登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return result;
        }
        scenicCommentService.commentThumNum(sid,userInfo.getId());
        return new AjaxResult(true,"").addData(scenicCommentService.getScenocComment(sid));
    }




       /* //点赞之前判断用户是否登录
        if(userInfo==null){
            AjaxResult result=new AjaxResult(false,"请先登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return  result;
        }else {
            boolean ret=scenicCommentStaticVOService.commentThumbUp(userInfo.g  etId(),sid);
            return  new AjaxResult(ret,"").addData(scenicCommentStaticVOService.getScenicCommentStaticVo(sid));
        }*/


}
